package com.shxc.smarttrade.old.adapternew;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.AlphaHistoryBean;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/10.
 */

public class AlphaHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<AlphaHistoryBean.DataBean.HistoryOrderBean> mbList;

    public AlphaHistoryAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<AlphaHistoryBean.DataBean.HistoryOrderBean> mbList) {
        this.mbList = mbList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PositionHistoryViewholder(mInflater.inflate(R.layout.adapter_style_position_history, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PositionHistoryViewholder) {
            AlphaHistoryBean.DataBean.HistoryOrderBean hb = mbList.get(position);
            ((PositionHistoryViewholder) holder).tvName.setText(hb.getSymbolCn());
            ((PositionHistoryViewholder) holder).tvEnName.setText(hb.getSymbolEn());
            if (hb.getCmd() == 0) {
                ((PositionHistoryViewholder) holder).tvCmd.setImageResource(R.mipmap.icon_buy);
//                ((PositionHistoryViewholder) holder).tvCmd.setText("涨");
//                ((PositionHistoryViewholder) holder).tvCmd.setBackgroundResource(R.drawable.blue_text_bg);
            } else {
                ((PositionHistoryViewholder) holder).tvCmd.setImageResource(R.mipmap.icon_sell);
//                ((PositionHistoryViewholder) holder).tvCmd.setText("跌");
//                ((PositionHistoryViewholder) holder).tvCmd.setBackgroundResource(R.drawable.orange_text_bg);
            }

            if (hb.getProfit() >= 0)
                ((PositionHistoryViewholder) holder).tvProfitLoss.setTextColor(mContext.getResources().getColor(R.color.quotation_orange_bg));
            else
                ((PositionHistoryViewholder) holder).tvProfitLoss.setTextColor(mContext.getResources().getColor(R.color.stock_fell_color));

            ((PositionHistoryViewholder) holder).tvProfitLoss.setText(BaseUtils.dealSymbol(hb.getProfit()));
            ((PositionHistoryViewholder) holder).tvBuyRisePrice.setText(hb.getOpenPrice() + "");
            ((PositionHistoryViewholder) holder).tvRealTimePrice.setText(hb.getClosePrice() + "");
            ((PositionHistoryViewholder) holder).tvPosStatus.setVisibility(View.GONE);
            ((PositionHistoryViewholder) holder).tvHands.setText(BaseUtils.getDigitsData(hb.getVolume(), 2) + "手");
            ((PositionHistoryViewholder) holder).tvBuyTime.setText("open：" + hb.getOpenTime());
            ((PositionHistoryViewholder) holder).tvSellTime.setText("close：" + hb.getCloseTime());

        }
    }

    @Override
    public int getItemCount() {
        return mbList == null ? 0 : mbList.size();
    }

    public class PositionHistoryViewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_cmd)
        ImageView tvCmd;
        @BindView(R.id.tv_buy_rise_price)
        TextView tvBuyRisePrice;
        @BindView(R.id.tv_real_time_price)
        TextView tvRealTimePrice;
        @BindView(R.id.tv_hands)
        TextView tvHands;
        @BindView(R.id.tv_profit_loss)
        TextView tvProfitLoss;
        @BindView(R.id.tv_buy_time)
        TextView tvBuyTime;
        @BindView(R.id.tv_sell_time)
        TextView tvSellTime;
        @BindView(R.id.tv_pos_status)
        TextView tvPosStatus;
        @BindView(R.id.tv_en_name)
        TextView tvEnName;

        PositionHistoryViewholder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }
}
